import { defineStore } from 'pinia'

const useMainStore = defineStore("main", {
    state: () => ({
        email: "",
        icpNumber: "",
        gaNumber: "",
        websiteName: "",
        name: "",
        captcha: false,
        allowUserPwd: false,
        expireTime: 60,
        domain: "",
        forceRefresh: false
    }),
    getters: {
        getWebInfo: state => ({
            email: state.email,
            icpNumber: state.icpNumber,
            gaNumber: state.gaNumber,
            websiteName: state.websiteName,
            name: state.name,
            captcha: state.captcha,
            allowUserPwd: state.allowUserPwd,
            expireTime: state.expireTime,
            domain: state.domain
        }),
        getForceRefresh() {
            const refresh = this.forceRefresh
            nextTick(() => {
                this.forceRefresh = false
            })
            return refresh
        }
    },
    actions: {
        setWebInfo(info) {
            this.$patch((state) => {
                state.email = info.email
                state.icpNumber = info.icpNumber
                state.gaNumber = info.gaNumber
                state.websiteName = info.websiteName
                state.domain = info.domain
                state.name = info.name
                state.captcha = info.captcha
                state.allowUserPwd = info.allowUserPwd
                state.expireTime = info.expireTime || 60
            })
        },
        setForceRefresh() {
            this.forceRefresh = true
        }
    },
    persist: {
        enabled: true,
        key: "webInfo",
        storage: localStorage,
        paths: ["websiteName"]
    }
})

export default useMainStore